# coding=utf-8
# -*- coding: UTF-8 -*-
#
# This file is part of the kernelstudio package.
#
# (c) 2014-2025 zlin <admin@kernelstudio.com>
#
# For the full copyright and license information, please view the LICENSE file
# that was distributed with this source code.

from ksai.persistence.storage.config import storage_config
from ksai.persistence.storage.core import StorageProvider, StorageProviderFactory
from ksai.persistence.storage.types import StorageType


class Storage:

    def __init__(self):
        super().__init__()
        self._type = storage_config.STORAGE_TYPE
        self._provider = self.initialize()

    def initialize(self):
        vector_store_provider_cls = self.get_provider_class(self._type)
        return vector_store_provider_cls().create()

    @staticmethod
    def get_provider_class(storage_type: str) -> type[StorageProviderFactory]:
        match storage_type:
            case StorageType.LOCAL:
                from ksai.persistence.storage.local import LocalStorageProviderFactory
                return LocalStorageProviderFactory
            case _:
                raise ValueError(f"Storage provider {storage_type} is not supported.")

    def save(self, filename, content) -> bool:
        return self._provider.save(filename, content)

    @property
    def provider(self) -> StorageProvider:
        return self._provider

    @property
    def type(self) -> str:
        return self._type


storage = Storage()
